package com.green.web.controller.manage;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.green.common.page.Pagination;
import com.green.model.manage.ComboSetup;
import com.green.model.manage.enums.ComboSetupTypeEnum;
import com.green.service.common.ServiceContext;
import com.green.service.manage.AreaService;
import com.green.service.manage.ComboSetupService;
import com.green.web.controller.common.BaseController;

/**
 * 设置套餐
 * @author 谷辉
 *
 */
@Controller
@RequestMapping(value = "/comboSetup/*")
public class ComboSetupController extends BaseController{

	@Resource
	private ComboSetupService comboSetupService;		

	@Resource
	private AreaService areaService;
	@RequestMapping(value = "manager")
	public String manager(ModelMap modelMap) {
		
		return "manage/ComboSetupManager";
	}
	/**
	 * @param modelMap套餐添加页面
	 * @return
	 */
	@RequestMapping(value = "add")
	public String add(ModelMap modelMap) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("level", 2);
		modelMap.put("areaList", this.areaService.getAreaList(param));
		return "manage/ComboSetupAdd";
	}
	
	/**
	 * @param modelMap保存套餐设置
	 * @return
	 */
	@RequestMapping(value = "save")
	@ResponseBody
	public Map<String, Object> save(ModelMap modelMap) {
		ComboSetup comboSetup=new ComboSetup();
		comboSetup.setName(getString("name"));
		comboSetup.setAreaName(getString("areaName"));
		comboSetup.setComboSetupType(ComboSetupTypeEnum.THESECONDARYPORT);
		comboSetup.setCreateBy(ServiceContext.getLoginPerson());
		comboSetup.setId(UUID.randomUUID().toString());
		comboSetup.setIsForbidden(1);
		comboSetup.setMeasure(getInt("measure"));
		comboSetup.setPrice(new BigDecimal(getString("price")));
		comboSetup.setRentingCount(getInt("rentingCount"));
		comboSetup.setRentRefurbish(getInt("rentRefurbish"));
		comboSetup.setSellHouse(getInt("sellHouse"));
		comboSetup.setSellRefurbish(getInt("sellRefurbish"));
		int count =comboSetupService.insertComboSetup(comboSetup);
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("message", count);
		return result;
	}
	/**
	 * 查询端口数据
	 * 
	 * @param modelMap
	 * @return
	 * @throws ParseException
	 */
	@RequestMapping("listData")
	@ResponseBody
	public void listData(Pagination<ComboSetup> page,HttpServletResponse response) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("personName", getString("personName"));
		param.put("isForbidden", getString("isForbidden"));
		
		page =comboSetupService.getComboSetupPagination(page, param);
        
		outPrint(response, JSONObject.fromObject(page, getDefaultJsonConfig()));
	}
	/**
	 * 禁用 
	 * 启用
	 * @return
	 * @throws ParseException
	 */
	@RequestMapping("deleteComboSetup")
	@ResponseBody
	public Map<String, Object> deleteComboSetup() {
		String id = getString("id");
		int type = getInt("type");
		int count = 0;
		if (null != id && !id.equals("")) {
			ComboSetup comboSetup=comboSetupService.getComboSetupById(id);
			comboSetup.setIsForbidden(type);
			comboSetup.setLastUpdateBy(ServiceContext.getLoginPerson());
			count=comboSetupService.updateComboSetup(comboSetup);
		}
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("message", count);
		return result;
	}
}
